<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="60%">
    <ContentWrap>
      <!-- 搜索工作栏 -->
      <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="68px"
      >
        <el-form-item label="老师姓名" prop="teacherName">
          <el-input
            v-model="queryParams.teacherName"
            placeholder="请输入老师姓名"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="老师编码" prop="teacherCode">
          <el-input
            v-model="queryParams.teacherCode"
            placeholder="请输入老师编码"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <CascadeSelection ref="selection" :useType="true" />
        <el-form-item label="性别" prop="sex">
          <el-select v-model="queryParams.sex" placeholder="请选择性别" clearable class="!w-240px">
            <el-option
              v-for="dict in getIntDictOptions(DICT_TYPE.SYSTEM_USER_SEX)"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
          <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>

    <!-- 列表 -->
    <ContentWrap>
      <el-table v-loading="loading" :data="list" @select="select" ref="dataTable">
        <el-table-column type="selection" width="55" />
        <el-table-column label="老师姓名" prop="teacherName" show-overflow-tooltip />
        <el-table-column label="老师编码" prop="teacherCode" show-overflow-tooltip />
        <el-table-column label="口令" prop="openId" show-overflow-tooltip />
        <el-table-column label="学校名称" prop="schoolName" show-overflow-tooltip />
        <el-table-column label="机构名称" prop="collegeName" show-overflow-tooltip />
        <el-table-column label="专业名称" prop="majorName" show-overflow-tooltip />
        <el-table-column label="班级名称" prop="classesName" show-overflow-tooltip />
        <el-table-column label="性别" prop="sex">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.SYSTEM_USER_SEX" :value="scope.row.sex" />
          </template>
        </el-table-column>
        <el-table-column
          label="创建时间"
          prop="createTime"
          :formatter="dateFormatter"
          show-overflow-tooltip
        />
      </el-table>
      <!-- 分页 -->
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="submitEnable">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import * as TeacherApi from '@/api/cmis/teacher'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('选择老师') // 弹窗的标题

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  createTime: [],
  teacherName: null,
  teacherCode: null,
  schoolName: null,
  collegeName: null,
  classesName: null,
  sex: null
})
const queryFormRef = ref() // 搜索的表单
const submitEnable = ref(true) //是否可以提交

const selection = ref()

/** 查询列表 */
const getList = async () => {
  // 接收子组件的数据
  const selectData = selection.value
    ? { ...queryParams, ...selection.value.selectData }
    : { ...queryParams }

  loading.value = true
  try {
    const data = await TeacherApi.getTeacherPage(selectData)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  selection.value?.resetFormData()
  queryFormRef.value?.resetFields()
  handleQuery()
}

/** 打开弹窗 */
const open = async () => {
  resetQuery()
  dialogVisible.value = true
}

const emit = defineEmits(['data'])
const submitForm = async () => {
  emit('data', dataTable.value.getSelectionRows())
  dialogVisible.value = false
}

const dataTable = ref()
const select = async (selection, row) => {
  // 没选值不能确认处理
  submitEnable.value = dataTable.value.getSelectionRows().length <= 0

  // 单选处理
  if (props.isSingleSelect) {
    dataTable.value.clearSelection()
    if (selection.length == 0) return
    dataTable.value.toggleRowSelection(row, true)
  }
}

const props = defineProps({
  isSingleSelect: Boolean
})

defineExpose({ open }) // 提供 open 方法，用于打开弹窗
</script>
